/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Category;
import pojo.Colors;
import pojo.Images;
import services.ServiceProduct;
import pojo.Product;
import pojo.Sizes;

/**
 *
 * @author DoaaRamiz
 */
public class ServletViewProduct extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ServiceProduct serviceProduct = new ServiceProduct();

        try {

            int productId = Integer.parseInt(request.getParameter("pId"));

            Product product = serviceProduct.productData(productId);
            if (product != null) {
                String productName = product.getName();
                String productBrand = product.getBrand();
                int productPrice = product.getPrice();
                String productDescription = product.getDescription();
                Category productCategoryId = product.getCategoryId();

                String productCategory = "";

                if (productCategoryId.getKids() == 0) {
                    if (productCategoryId.getGender().equals("female")) {
                        productCategory += "Women - ";
                    } else if (productCategoryId.getGender().equals("male")) {
                        productCategory += "Men - ";
                    }

                } else if (productCategoryId.getKids() == 1) {
                    if (productCategoryId.getGender().equals("female")) {
                        productCategory += "Kids - Girls - ";
                    } else if (productCategoryId.getGender().equals("male")) {
                        productCategory += "Kids - Boys - ";
                    }
                }

                productCategory += productCategoryId.getDescription();

                List<Colors> productColorsList = product.getColorsList();
                String productColors = "Colors: ";
                for (Colors colors : productColorsList) {
                    productColors += colors.getColor() + " ";
                }

                List<Sizes> productSizesList = product.getSizesList();
                String productSizes = "Sizes: ";
                for (Sizes sizes : productSizesList) {
                    productSizes += sizes.getSize() + " ";
                }

                List<Images> productImagesList = product.getImagesList();
                String productImage = productImagesList.get(0).getLocation();

                String html = "<div class=\"image\">\n"
                        + "<img class=\"slideimage\" src=\"" + productImage + "\"/>\n"
                        + "</div >\n"
                        + "<div class=\"pdata\" >\n"
                        + "<h3>Name: " + productName + "</h3>\n"
                        + "<h4>Brand: " + productBrand + "</h4>\n"
                        + "<h4>Category: " + productCategory + "</h4>\n"
                        + "<h4>Price: L.E. " + productPrice + "</h4>\n"
                        + "<p class=\"title\">" + productSizes + "   ---   " + productColors + "</p>\n"
                        + "<p class=\"description\">" + productDescription + "</p>\n"
                        + "<a class=\"read-more-btn\" onClick=\"addToCart(" + productId + ")\" > Add to cart</a>\n"
                        + "</div>";

                out.print(html);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
